package com.connector.server.util;

import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Set;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Maps;
import com.connector.base.message.model.ChatMessage;
import com.google.common.collect.Sets;
import com.alibaba.fastjson.JSON;
import com.connector.base.connection.Connection;
import com.connector.base.connection.channel.SocketChannel;
import java.util.concurrent.ConcurrentMap;
import com.connector.server.jmx.JmxMessageBean;
import org.slf4j.Logger;

public class SendMsgUtilThread implements Runnable
{
    private static final Logger log;
    private JmxMessageBean jmxMessageBean;
    private ConcurrentMap<SocketChannel, Connection> allConnection;

    public SendMsgUtilThread(final JmxMessageBean jmxMessageBean, final ConcurrentMap<SocketChannel, Connection> allConnection) {
        this.jmxMessageBean = jmxMessageBean;
        this.allConnection = allConnection;
    }

    public void sendMsg(final JmxMessageBean messageBean, final ConcurrentMap<SocketChannel, Connection> cons) {
        final String[] dialogIds = messageBean.getDialogId().split(",");
        final String[] sendTypes = messageBean.getSendType().split(",");
        final String[] targetIdS = messageBean.getTargetId().split(",");
        int k = 0;
        for (final String dialogId : dialogIds) {
            messageBean.setDialogId(dialogId);
            messageBean.setSendType(sendTypes[k]);
            messageBean.setTargetId(targetIdS[k]);
            SendMsgUtilThread.log.info("开始处理消息:" + JSON.toJSONString(messageBean));
            final String sendType = messageBean.getSendType();
            Set<String> targetIds =  Sets.newHashSet();
            String msgJson = "";
            String sendUserId = "";
            if (sendType.equals("1")) {
                sendUserId = messageBean.getUserId();
                final ChatMessage chatMessage = new ChatMessage();
                chatMessage.setDeviceId(messageBean.getDeviceId());
                chatMessage.setUserId(messageBean.getUserId());
                chatMessage.setBody(messageBean.getText());
                chatMessage.setContentType(messageBean.getTextType());
                chatMessage.setType("1");
                chatMessage.setDate(messageBean.getDate());
                chatMessage.setDialogId(messageBean.getDialogId());
                chatMessage.setReceiver(messageBean.getTargetId());
                chatMessage.setPhoto(messageBean.getPhoto());
                chatMessage.setOtherText(messageBean.getOtherText());
                chatMessage.setFileName(messageBean.getFileName());
                chatMessage.setUserName(messageBean.getUserName());
                chatMessage.setMsgUID(messageBean.getMsgUID());
                chatMessage.setAudioTimeLength(messageBean.getAudioTimeLength());
                chatMessage.setReadingStatus(Boolean.valueOf(false));
                targetIds = Sets.newHashSet(messageBean.getTargetId().split(","));
                targetIds.add(sendUserId);
                if (messageBean.getTextType().equals("4")) {
                    chatMessage.setReadingStatus(Boolean.valueOf(false));
                }
                msgJson = JSON.toJSONString((Object)chatMessage);
            }
            if (sendType.equals("3")) {
                sendUserId = messageBean.getUserId();
                final ChatMessage chatMessage = new ChatMessage();
                chatMessage.setDeviceId(messageBean.getDeviceId());
                chatMessage.setUserId(messageBean.getUserId());
                chatMessage.setBody(messageBean.getText());
                chatMessage.setContentType(messageBean.getTextType());
                chatMessage.setType("3");
                chatMessage.setDate(messageBean.getDate());
                chatMessage.setDialogId(messageBean.getDialogId());
                chatMessage.setPhoto(messageBean.getPhoto());
                chatMessage.setReceiver(messageBean.getTargetId());
                chatMessage.setOtherText(messageBean.getOtherText());
                chatMessage.setFileName(messageBean.getFileName());
                chatMessage.setUserName(messageBean.getUserName());
                chatMessage.setMsgUID(messageBean.getMsgUID());
                chatMessage.setAudioTimeLength(messageBean.getAudioTimeLength());
                final Map<String, String> paramMap = Maps.newHashMap();
                paramMap.put("groupId", messageBean.getTargetId());
                paramMap.put("userId", messageBean.getUserId());
                try {
                    final String resultData = HttpUtil.postToInstantServer_getGroupDetail(paramMap);
                    final HttpResultBean httpResultBean = (HttpResultBean)JSON.parseObject(resultData, (Class)HttpResultBean.class);
                    final JSONArray jsonArray = (JSONArray)httpResultBean.getObj();
                    for (int i = 0; i < jsonArray.size(); ++i) {
                        final JSONObject jsonObject = (JSONObject)jsonArray.get(i);
                        final String userId = jsonObject.getString("userId");
                        targetIds.add(userId);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                if (messageBean.getTextType().equals("4")) {
                    chatMessage.setReadingStatus(Boolean.valueOf(false));
                }
                msgJson = JSON.toJSONString((Object)chatMessage);
            }
            SendUtil.sendMsg(messageBean, Sets.newHashSet((Iterable)targetIds), ChatMessage.buildPacket(msgJson), cons, msgJson, sendUserId);
            ++k;
        }
    }

    @Override
    public void run() {
        this.sendMsg(this.jmxMessageBean, this.allConnection);
    }

    public void setJmxMessageBean(final JmxMessageBean jmxMessageBean) {
        this.jmxMessageBean = jmxMessageBean;
    }

    public void setAllConnection(final ConcurrentMap<SocketChannel, Connection> allConnection) {
        this.allConnection = allConnection;
    }

    public JmxMessageBean getJmxMessageBean() {
        return this.jmxMessageBean;
    }

    public ConcurrentMap<SocketChannel, Connection> getAllConnection() {
        return this.allConnection;
    }

    static {
        log = LoggerFactory.getLogger((Class)SendMsgUtilThread.class);
    }
}
